/*
 * tree_as_list.h
 *
 *  Created on: May 21, 2013
 *      Author: ILYA
 *
 *  Description: User must create one instance of TreeNode, initialize it with init_treeNode function ,
 *  				and always use it instance for calling functions.
 *  				This instance has LinkID equal to 255, and that is condition
 *  				that no one else LinkID couldn't be equal to 255.
 *  			 All functions which is manipulating with "struct ListRIMFrames value", must use only
 *  			 	two fields which belong this struct. The fields are:
 *  					struct UniqueSerialNumber serialNumber;;
						unsigned char Link_ID;
//this module can be use one treeNode instance, because there is only one variable flash_address
*/

#ifndef TREE_AS_LIST_H_
#define TREE_AS_LIST_H_
#include "llc.h"

#define ROOT_LINK_ID 255
#define POINTER_ON_NULL 255
/*the time unique, which we define as equal to 1*/
#define T_UNIQUE 200 //eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
//extern int flash_address; //now this field is not used by any other module
/*arp_current_time must be updated from another module, time by time*/
//extern unsigned char arp_current_time; //now this field is not used by any other module


typedef enum {left, right, top, next, prev} relational_address;

typedef struct s_TreeNode{
	struct UniqueSerialNumber serialNumber;
	unsigned char Link_ID;
	/*the LinkID of left TreeNode*/
    unsigned char left;
    /*the LinkID of right TreeNode*/
    unsigned char right;
    /*the LinkID of top TreeNode*/
    unsigned char top;

    /*pointed to the next added x_any TreeNode, this mean that ("x_any->top")->next != (this)->LinkID*/
    unsigned char next;
    /*pointed to the preview x_any TreeNode, this mean that ("x_any->prev")->next == (this)->LinkID*/
    unsigned char prev;
    /*the relational time, when this TreeNode was arrived*/
    unsigned char timer;
} TreeNode;
typedef struct s_TreeNode TreeNode2Field;

/*before using anything from this module you must call this function*/
void init_treeNode(int address, TreeNode **return_x);
/*return created TreeNode*/
void insert_newNode(Timer_Tik current_time, TreeNode2Field value, TreeNode *x, TreeNode **return_x);
/*return created TreeNode, don't use this function by another module*/
void insert_treeNode(TreeNode2Field value, TreeNode *x, char isTimerOn, TreeNode **return_x);
/*getting treeNode by serial number*/
void get_treeNode(struct UniqueSerialNumber value_sn, TreeNode *x, TreeNode **return_x);
/*
//void delete_treeNode(TreeNode2Field value, TreeNode *x);
//this function delete the value tree node and its chains from x, into flash
//call this function, when you sure, that device (which node you want to delete) is disconnected
*/
void delete_treeNode(TreeNode2Field value, TreeNode *x);
/*
//void delete_treeNode_byLinkID(unsigned char Link_ID, TreeNode *x)
//this function delete the tree node (by LinkID) and its chains from x, into flash
//call this function, when you sure, that device (which node you want to delete) is disconnected
*/
void delete_treeNode_byLinkID(unsigned char Link_ID);
/*
//call "void delete_Nodes_greater_x_time(char x_time);" function, if you need to delete
//all nodes, which lying into arp table long time than x_time
*/
void delete_Nodes_greater_x_time(char x_time);
void free_treeNode(TreeNode **x);
void get_fromFlash(TreeNode *treeNode, relational_address pos, TreeNode **return_x);
void write_intoFlash(TreeNode *treeNode);

/*call this function by giving it LinkID as address, if there is not any node by this LinkID,
//this function would return 0 pointer
 */
void read_fromFlash(unsigned char address, TreeNode **return_x);

#endif /* TREE_AS_LIST_H_ */
